package com.lou1052.data.datesource;//package com.lou1052.data.core.datesource;
//
//import org.slf4j.LoggerFactory;
//
//import javax.sql.DataSource;
//import java.io.Closeable;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.sql.Connection;
//import java.sql.SQLException;
//import java.sql.SQLFeatureNotSupportedException;
//import java.util.logging.Logger;
//
///**
// * 读写分离数据源
// * Created by liuqing-notebook on 2016/7/6.
// */
//public class ReadWriteDataSource implements DataSource ,Closeable {
//
//    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadWriteDataSource.class);
//
//    private DataSource readDataSource;
//
//    private DataSource writeDataSource;
//
//    @Override
//    public Connection getConnection() throws SQLException {
//        Boolean isReadOnly = ThreadLocalReadOnly.LOCAL_READ_ONLY.get();
//        if(isReadOnly==null){
//            LOGGER.warn("ThreadLocalReadOnly is null! use writeDataSource");
//            return writeDataSource.getConnection();
//        }
//        if(isReadOnly){
//            LOGGER.info("use readDataSource");
//            return readDataSource.getConnection();
//        }
//        LOGGER.info("use writeDataSource");
//        return writeDataSource.getConnection();
//    }
//
//    @Override
//    public Connection getConnection(String username, String password) throws SQLException {
//        Boolean isReadOnly = ThreadLocalReadOnly.LOCAL_READ_ONLY.get();
//        if(isReadOnly==null){
//            LOGGER.warn("ThreadLocalReadOnly is null! use writeDataSource");
//            return writeDataSource.getConnection(username,password);
//        }
//        if(isReadOnly){
//            LOGGER.info("use readDataSource");
//            return readDataSource.getConnection(username,password);
//        }
//        LOGGER.info("use writeDataSource");
//        return writeDataSource.getConnection(username,password);
//    }
//
//    @Override
//    public PrintWriter getLogWriter() throws SQLException {
//        return writeDataSource.getLogWriter();
//    }
//
//    @Override
//    public void setLogWriter(PrintWriter out) throws SQLException {
//        writeDataSource.setLogWriter(out);
//        readDataSource.setLogWriter(out);
//    }
//
//    @Override
//    public void setLoginTimeout(int seconds) throws SQLException {
//        writeDataSource.setLoginTimeout(seconds);
//        readDataSource.setLoginTimeout(seconds);
//    }
//
//    @Override
//    public int getLoginTimeout() throws SQLException {
//        return writeDataSource.getLoginTimeout();
//    }
//
//    @Override
//    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
//        return writeDataSource.getParentLogger();
//    }
//
//    @Override
//    public <T> T unwrap(Class<T> iface) throws SQLException {
//        return null;
//    }
//
//    @Override
//    public boolean isWrapperFor(Class<?> iface) throws SQLException {
//        return false;
//    }
//
//    public DataSource getReadDataSource() {
//        return readDataSource;
//    }
//
//    public void setReadDataSource(DataSource readDataSource) {
//        this.readDataSource = readDataSource;
//    }
//
//    public DataSource getWriteDataSource() {
//        return writeDataSource;
//    }
//
//    public void setWriteDataSource(DataSource writeDataSource) {
//        this.writeDataSource = writeDataSource;
//    }
//
//
//    @Override
//    public void close() throws IOException {
//        IOException exception = null;
//        try {
//            if (writeDataSource instanceof Closeable) {
//                ((Closeable) writeDataSource).close();
//            }
//        }catch (IOException e){
//            LOGGER.error("writeDataSource.close() error!",e);
//            exception=e;
//        }
//        try {
//            if (readDataSource instanceof Closeable) {
//                ((Closeable) readDataSource).close();
//            }
//        }catch (IOException e){
//            LOGGER.error("readDataSource.close() error!",e);
//            exception=e;
//        }
//        if(exception!=null){
//            throw exception;
//        }
//    }
//}
